package com.exam.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.exam.dto.ChangePwdParam;
import com.exam.dto.RegisterParam;
import com.exam.entity.Admin;
import com.exam.entity.Student;
import com.exam.entity.Teacher;
import com.exam.mapper.LoginMapper;
import com.exam.mapper.StudentMapper;
import com.exam.mapper.TeacherMapper;
import com.exam.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapper loginMapper;

    @Resource
    private TeacherMapper teacherMapper;

    @Resource
    private StudentMapper studentMapper;

    @Override
    public Admin adminLogin(String username, String password) {
        return loginMapper.adminLogin(username,password);
    }

    @Override
    public Teacher teacherLogin(String mobile, String password) {
        return loginMapper.teacherLogin(mobile,password);
    }

    @Override
    public Student studentLogin(String userMobile, String password) {
        return loginMapper.studentLogin(userMobile,password);
    }

    @Override
    public String register(RegisterParam registerParam) {
        if (registerParam.getRole() == 1){
            teacherMapper.insert(new Teacher(
                    registerParam.getId(),
                    registerParam.getUsername(),
                    registerParam.getMobile(),
                    registerParam.getPassword()
                    ,registerParam.getRole()));
        }else {
            studentMapper.insert(new Student(registerParam.getId(),
                    registerParam.getUsername(),
                    registerParam.getMobile(),
                    registerParam.getPassword(),
                    registerParam.getRole()
            ));
        }

        return "注册成功";


    }

    @Override
    public Integer findByPwd(Long studentId, String password) {
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Student::getStudentId,studentId);
        queryWrapper.eq(Student::getPwd,password);
        return studentMapper.selectCount(queryWrapper);
    }

    @Override
    public void changePwd(ChangePwdParam changePwdParam) {
        LambdaUpdateWrapper<Student> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Student::getStudentId,changePwdParam.getId());
        studentMapper.update(new Student().setStudentId(changePwdParam.getId()).setPwd(changePwdParam.getNewPassword()),updateWrapper);
    }

    @Override
    public Integer findById(Long id) {
        return studentMapper.selectCount(new LambdaQueryWrapper<Student>().eq(Student::getStudentId,id));
    }

    @Override
    public Integer findByPwdTea(Long id, String oldPassword) {
        LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Teacher::getTeacherId,id);
        queryWrapper.eq(Teacher::getPwd,oldPassword);
        return teacherMapper.selectCount(queryWrapper);
    }

    @Override
    public void changePwdTea(ChangePwdParam changePwdParam) {
        LambdaUpdateWrapper<Teacher> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Teacher::getTeacherId,changePwdParam.getId());
        teacherMapper.update(new Teacher().setTeacherId(changePwdParam.getId()).setPwd(changePwdParam.getNewPassword()),updateWrapper);

    }
}
